#include "chatcommand_simplegamechat.h"
#include "websocket.h"
#include "datastring.h"
#include "parameters.h"
#include "message.h"
#include "Debug.h"
#include <inttypes.h>
#include "simplechatgame.h"

// simplegamechat(messageid,gameid,username,message)
// Sends a chat message to usename in a simple game. Sends a simplegamechat message to the user and a success message or an error message to the caller.
bool chatcommand_simplegamechat::processmessage(char first_letter,message *received_message,chatclient *client)
{
	if ((first_letter != 's') 
		|| (received_message->actual_message.substr(0,15)!="simplegamechat(")) {
		return false;
	}	
	
	Debug debug(__FILE__,__func__,__LINE__);
	message *new_message;
	datastring method_parameters;
	parameters parameters_parsed;
	bool parameter_success = true;
	int64_t messageid;
	datastring gameid;
	datastring usernameto;
	datastring chatmessage;
	simplechatgame *game;
	datastring error_message;
	datastring *usernamefrom;
	chatclient *chatclientto;
	biglist_item<simplechatgameuser *> *usersearchfrom;
	biglist_item<simplechatgameuser *> *usersearchto;
	biglist_item<simplechatgame *> *gamesearch;
	
	debug = __LINE__;
	method_parameters = received_message->actual_message.substr(15,received_message->actual_message.length-16);
	method_parameters.null_terminate(); // Used to comply with snprintf.
	parameters_parsed.long_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
		
	debug = __LINE__;
	if (parameter_success) {
		// Give the parameters nice names.
		debug = __LINE__;
		messageid = parameters_parsed.long_parameters[0];
		gameid = parameters_parsed.string_parameters[1];
		usernameto = parameters_parsed.string_parameters[2];		
		chatmessage = parameters_parsed.string_parameters[3];
	
		debug = __LINE__;
		// Look to see if this gameid is already used.
		gamesearch = the_websocket->find_simple_game(gameid);
		if (gamesearch == nullptr) {
			error_message = "That gameid was not found.";
			error(client,error_message,messageid);
			return true;
		}
		game = gamesearch->item;
		debug = __LINE__;
		// Look for this user in this game.
		usersearchfrom = game->getuser(client);
		if (usersearchfrom == nullptr) {
			error_message = "You're not in that game.";
			error(client,error_message,messageid);
			return true;
		}
		usernamefrom = usersearchfrom->item->username;
		// Look for usernameto if it's specified.
		if (usernameto.length == 0) {
			// They didn't specify a usernameto, so send the message to all the users in the game.
			new_message = simplegamechat(messageid,gameid,*usernamefrom,chatmessage);
			game->send_message_to_clients(new_message,nullptr);
			idisposable::dereference((idisposable**)&new_message);
		} else {
			usersearchto = game->getuser(usernameto);
			if (usersearchto == nullptr) {
				error_message = "That user is not in that game.";
				error(client,error_message,messageid);
				return true;
			}
			// Try to send a message to that user.
			chatclientto = usersearchto->item->client;
			if (chatclientto == nullptr) {
				error_message = "That user is currently disconnected.";
				error(client,error_message,messageid);
			} else {
				new_message = simplegamechat(messageid,gameid,*usernamefrom,chatmessage);
				client->add_message(new_message);
				client->callback_on_writable();
				chatclientto->add_message(new_message);
				chatclientto->callback_on_writable();
				idisposable::dereference((idisposable**)&new_message);
			}
		}
		
	} else {
		debug = __LINE__;
		parameters_not_correct(client,received_message);
	}
	debug = __LINE__;
	return true;
}
